Ext.define('AmpleHarvest.objects.harvestrequest.view.window.request.Form',
{
   extend      : 'Ext.form.Panel',
   requires    :
   [
         "Ext.form.field.Hidden", 
         "Ext.form.field.Text",
         "Ext.form.field.Checkbox", 
         "Ext.form.field.TextArea",
         "Ext.form.field.ComboBox",
         "AmpleHarvest.objects.classification.cPanel"
   ],
   uses        : [
   // 'DarkOwl.User_Manager.Windows.Setup.cCreateDB',
   ],
   m_bool_Editable   : false,
   labelWidth     : 93,
   buttonAlign    : 'right',
   frame       : true,
   timeout        : 1000,
   bodyStyle      : 'padding:5px 5px 0',
   autoWidth      : true,
   autoHeight     : true,
   standardSubmit : false,
   defaultType    : 'textfield',

   initComponent  : function()
   {
      var obj_This = this;

      if (this.m_bool_Editable)
      {
         this.buttons =
         [
               {
                  text     : 'Cancel',
                  autoWidth   : true,
                  disabled : false,
                  handler     : function()
                  {
                     obj_This.ownerCt.destroy();
                  }
               },
               {
                  text     : 'Submit',
                  formBind : true,
                  disabled : true,
                  scope    : this,
                  autoWidth   : true,
                  handler     : function()
                  {

                     obj_This.getForm().submit(
                     {
                        url      : obj_config.C_STR_EXTERNAL_PATH+'/HarvestRequest/saveHarvestRequest.php',
                        waitMsg  : 'Submitting Your Harvest Request',
                        success  : this.submit_Success,
                        failure  : this.submit_Fail,
                        scope : this
                     }
                     );
                  }
               }
         ];
      }
      else
      {
         this.buttons =
         [
               {
                  text     : 'Close',
                  autoWidth   : true,
                  disabled : false,
                  handler     : function()
                  {
                     obj_This.ownerCt.close();
                  }
               }
         ];
      }

      this.callParent();
      this.m_str_UserName = obj_config.user.userName;
 
      this.m_obj_Classification = Ext.create("AmpleHarvest.objects.classification.cPanel",
            {
               m_bool_Editable            : this.m_bool_Editable,
               m_str_ClassificationName   : "Classification",
               m_str_SCIName           : "SCI",
               m_str_DistroName        : "Distro"
            }
      );

      this.add(this.m_obj_Classification);

      this.m_obj_Form_UserID = Ext.create("Ext.form.field.Hidden",
      {
         name     : "User GUID",
         readOnly : true,
         allowBlank  : false
      }
      );
      this.m_obj_Form_UserID.setValue(obj_config.user.id);
      
      this.add(this.m_obj_Form_UserID);

      this.m_obj_Form_RequestID = Ext.create("Ext.form.field.Text",
      {
         name     : "id",
         fieldLabel  : "ID",
         anchor      : "100%",
         hidden      : true,
         readOnly : true
         // allowBlank : false
      }
      );
      this.add(this.m_obj_Form_RequestID);

      this.m_obj_Form_RequestNum = Ext.create("Ext.form.field.Text",
      {
         name     : "requestNum",
         fieldLabel  : "Request #",
         anchor      : "100%",
         hidden      : true,
         readOnly : true
            // allowBlank : false
      }
      );
      this.add(this.m_obj_Form_RequestNum);

      this.m_obj_Form_UserName = Ext.create("Ext.form.field.Text",
            {
               fieldLabel  : "User",
               name     : "User_Name",
               inputType   : "textfield",
               disabled : false,
               anchor      : "100%",
               readOnly : !this.m_bool_Editable,
               allowBlank  : false
            });
      this.m_obj_Form_UserName.setValue(obj_config.user.userName);
      this.add(this.m_obj_Form_UserName);

      this.add(Ext.create("Ext.form.field.Text",
            {
               fieldLabel  : "Source URL",
               name     : "url",
               inputType   : "textfield",
               anchor      : "93%",
               readOnly : !this.m_bool_Editable,
               allowBlank  : false
               //,value    : g_arr_Requests["<?php print($arr_Columns[$obj_TABLE_Harvest_Request->m_int_COL_URL]->get_ColName()) ?>"]
            }
            )
      );

      this.add(Ext.create("Ext.form.field.Text",
            {
               fieldLabel  : "Language",
               name     : "language",
               inputType   : "textfield",
               anchor      : "93%",
               readOnly : !this.m_bool_Editable,
               allowBlank  : true
               //,value    : g_arr_Requests["<?php print($arr_Columns[$obj_TABLE_Harvest_Request->m_int_COL_Language]->get_ColName()) ?>"]
            }
            )
      );

      this.m_obj_Status = Ext.create("Ext.form.field.ComboBox",
      {
         fieldLabel     : "Status",
         name           : "status",
         hiddenName     : "status",
         disabled    : false,
         hidden         : true,
         readOnly    : !this.m_bool_Editable,
         store       : Ext.create("AmpleHarvest.DATA.Store.cDropDown"),
         autoWidth      : true,
         minChars    : 1,
         displayField   : 'Label',
         valueField     : 'Value',
         allowBlank     : true,
         readOnly    : !this.m_bool_Editable,
         autoSelect     : true,
         typeAhead      : true,
         queryMode      : 'remote',
         forceSelection : true,
         triggerAction  : 'all',
         emptyText      : 'Status?',
         selectOnFocus  : true
      }
      );
      
      this.m_obj_Status.store.set_ExtraParams(
      {
         id : "4A669C97-65F0-4E89-B818-9B4249EC1E13" //"<?php print(cTABLE_Drop_Down::C_STR_DROPDOWN_HR_STATUS);?>"
      }
      );

      this.m_obj_Status.setRawValue(0);
      this.add(this.m_obj_Status);

      this.add(Ext.create("Ext.form.field.TextArea",
            {
               name     : "description",
               fieldLabel  : "Description",
               inputType   : "textfield",
               readOnly : !this.m_bool_Editable,
               anchor      : '93%',
               allowBlank  : false
               //value    : g_arr_Requests["description"]
            }
            )
      );

      this.add(Ext.create("Ext.form.field.TextArea",
            {
               name     : "key_words",
               fieldLabel  : "Keywords",
               inputType   : "textfield",
               readOnly : !this.m_bool_Editable,
               anchor      : '93%',
               allowBlank  : false,
               validator   : function(mix_Value)
               {
                  if (obj_This.m_obj_WholeSite)
                  {
                     if (obj_This.m_obj_WholeSite.getValue() == 0)
                     {
                        if (mix_Value == "")
                        {
                           return "This field can not be blank.";
                        }
                     }
                  }
                  return true;
               }
               //value    : g_arr_Requests["key_words"]
            }
            )
      );

      this.add(Ext.create("Ext.form.field.TextArea",
      {
         fieldLabel  : "Justification",
         name     : "reason",
         allowBlank  : false,
         readOnly : !this.m_bool_Editable,
         anchor      : '93%'
         //value    : g_arr_Requests["reason"]
      }
      ));

      this.add(Ext.create("Ext.form.field.TextArea",
      {
         fieldLabel  : "Comment",
         name     : "comment",
         anchor      : '93%',
         readOnly : !this.m_bool_Editable,
         allowBlank  : true
         //value    : g_arr_Requests["comment"]
      }
      ));

      this.m_obj_Sensitivity = Ext.create("Ext.form.field.ComboBox",
      {
         fieldLabel     : "Sensitivity",
         name        : "sensitivity",
         hiddenName     : "sensitivity",
         disabled    : false,
         anchor         : "93%",
         readOnly    : !this.m_bool_Editable,
         store       : Ext.create("AmpleHarvest.DATA.Store.cDropDown"),
         displayField   : 'Label',
         valueField     : 'Value',
         allowBlank     : false,
         readOnly    : !this.m_bool_Editable,
         autoSelect     : true,
         typeAhead      : true,
         minChars    : 1,
         mode        : 'remote',
         forceSelection : true,
         triggerAction  : 'all',
         emptyText      : 'Sensitive?',
         selectOnFocus  : true
      }
      );

      this.m_obj_Sensitivity.store.set_ExtraParams(
      {
         id : "7C4348E5-09C8-4073-884C-ACB848D43730" //"<?php print(cTABLE_Drop_Down::C_STR_DROPDOWN_DEFAULT_YESNO); ?>"
      }
      );

      this.add(this.m_obj_Sensitivity);

      this.m_obj_Risk = Ext.create("Ext.form.field.TextArea",
      {
         fieldLabel  : "Risk",
         name     : "risk",
         anchor      : '93%',
         readOnly : !this.m_bool_Editable,
         allowBlank  : true,
         validator   : function(mix_Value)
         {
            if (obj_This.m_obj_Sensitivity)
            {
               if (obj_This.m_obj_Sensitivity.getValue() != 0)
               {
                  if (mix_Value == "")
                  {
                     return "This field can not be blank.";
                  }
               }

            }
            return true;
         }
      }
      );

      this.add(this.m_obj_Risk);

      this.m_obj_Type = Ext.create("Ext.form.field.ComboBox",
      {
         fieldLabel     : "Type",
         name        : "type",
         hiddenName     : "type",
         disabled    : false,
         anchor         : "93%",
         readOnly    : !this.m_bool_Editable,
         store       : Ext.create("AmpleHarvest.DATA.Store.cDropDown"),
         readOnly    : !this.m_bool_Editable,
         displayField   : 'Label',
         valueField     : 'Value',
         allowBlank     : false,
         autoSelect     : true,
         typeAhead      : true,
         minChars    : 1,
         mode        : 'remote',
         forceSelection : true,
         triggerAction  : 'all',
         emptyText      : 'Type',
         selectOnFocus  : true
      }
      );

      this.m_obj_Type.store.set_ExtraParams(
      {
         id: '7979D1A5-3758-4C94-8D5E-AE6DAF5A841A' //<?php   print($obj_TABLE_Drop_Down->get_Column_Name($obj_TABLE_Drop_Down->m_int_COL_ID)); ?> : "<?php print(cTABLE_Drop_Down::C_STR_DROPDOWN_HR_TYPE); ?>"
      }
      );

      this.add(this.m_obj_Type);

      this.m_obj_Frequency = Ext.create("Ext.form.field.ComboBox",
      {
         fieldLabel     : "Frequency",
         name        : "frequency",
         hiddenName     : "frequency",
         disabled    : false,
         anchor         : "93%",
         readOnly    : !this.m_bool_Editable,
         store       : Ext.create("AmpleHarvest.DATA.Store.cDropDown"),
         displayField   : 'Label',
         valueField     : 'Value',
         allowBlank     : false,
         autoSelect     : true,
         typeAhead      : true,
         minChars    : 1,
         mode        : 'remote',
         forceSelection : true,
         triggerAction  : 'all',
         emptyText      : 'Frequency',
         selectOnFocus  : true
      }
      );

      this.m_obj_Frequency.store.set_ExtraParams(
      {
         id : 'A2D0E0B8-88FA-4D8C-B162-B5F90B32FCB8' //<?php   print($obj_TABLE_Drop_Down->get_Column_Name($obj_TABLE_Drop_Down->m_int_COL_ID)); ?> : "<?php print(cTABLE_Drop_Down::C_STR_DROPDOWN_HR_FREQUENCY); ?>"
      }
      );

      
      this.add(this.m_obj_Frequency);
      
      //Alternate to defining it all here
      //this.add(Ext.create('AmpleHarvest.objects.harvestrequest.view.combobox.Frequency', {m_bool_Editable: this.m_bool_Editable}) );

      this.m_obj_Discussion = Ext.create("Ext.form.field.TextArea",
      {
         fieldLabel  : "Discussion",
         name     : "discussion",
         anchor      : '93%',
         hidden      : true,
         readOnly : !this.m_bool_Editable,
         allowBlank  : true
      }
      );

      this.add(this.m_obj_Discussion);

      if (this.m_bool_Editable)
      {
         this.add(Ext.create("Ext.panel.Panel",
         {
            frame : true,
            border   : "0",
            html  : '* Field labels "<span style="color:red"><u>red</u></span>" are required, Also you must select either Whole Site or Enter Keywords.'
         }
         ));
      }
   },

   set_RequestID  : function(str_ID)
   {
      var obj_This = this;
      this.m_str_RequestID = str_ID;

      this.m_obj_Classification.initDropDowns();
      this.m_obj_Status.doQuery("", true);
      this.m_obj_Status.collapse();
      this.m_obj_Status.setValue(0);
      this.m_obj_Sensitivity.doQuery("", true);
      this.m_obj_Sensitivity.collapse();
      this.m_obj_Type.doQuery("", true);
      this.m_obj_Type.collapse();
      this.m_obj_Frequency.doQuery("", true);
      this.m_obj_Frequency.collapse();

      if (this.m_str_RequestID)
      {
         this.m_obj_Status.show();
         this.m_obj_Discussion.show();
         this.m_obj_Form_RequestID.show();
         this.m_obj_Form_RequestNum.show();

         this.load(
         {
            url      : obj_config.C_STR_EXTERNAL_PATH+'/HarvestRequest/get_Request_JSON.php',
            /*params   :
            {
               <?php print($obj_TABLE_Harvest_Request->get_TableName());
                     print("_");
                     print($obj_TABLE_Harvest_Request->get_Column_Name($obj_TABLE_Harvest_Request->m_int_COL_ID));
                  ?> : this.m_str_RequestID
            },*/
            params   :
            {
                     Harvest_Requests_id: this.m_str_RequestID
            },
            waitMsg  : 'Loading Harvest Data',
            success  : function(obj_Response, obj_Result, obj_Type)
            {
               obj_This.m_obj_Classification.setInitClass(obj_Result.result.data.classification);
               obj_This.m_obj_Classification.setInitSCI(obj_Result.result.data.sci);
               obj_This.m_obj_Classification.setInitDistro(obj_Result.result.data.distro);
               obj_This.m_obj_Classification.initClassification();
            }
         }
         );
      }
   },

   submit_Success : function(str_Function, mix_Arg)
   {
	  m_obj_AmpleHarvest_ViewPort.down('ampleharvest-objects-harvestrequest-view-harvestrequest').refreshData();
      this.ownerCt.close();

      Ext.MessageBox.alert("Success", "Your Request has been sent.", close);
   },

   submit_Fail    : function(str_Function, mix_Arg)
   {
	   if (typeof mix_Arg.result === 'undefined') {
		   console.log(mix_Arg);
		   Ext.MessageBox.alert("Error", mix_Arg.response.status+": "+mix_Arg.response.statusText, close);
		   
	   } else {
		   if(! mix_Arg.result.errors.fatal) {
			   this.ownerCt.close();
			   Ext.MessageBox.alert("Result",
					   "Your Request has been sent.<br><br>"
						   +"The following non-fatal error was encountered:<br>"
						   +mix_Arg.result.errors.msg,
					   close);
		   } else {
			   Ext.MessageBox.alert("Error", mix_Arg.result.errors.msg, close);
		   }

	   }
       m_obj_AmpleHarvest_ViewPort.down('ampleharvest-objects-harvestrequest-view-harvestrequest').refreshData();
   }
}
);
